package com.qqtech.qquan.pub.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.BizzLevel1Enum;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.circle.dao.CircleTopicDao;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.model.CircleTopicVo;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.dao.CommunityMessageDao;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.hi.service.HiCircleService;
import com.qqtech.qquan.hi.service.HiCommunityService;
import com.qqtech.qquan.idle.model.IdleBaseVo;
import com.qqtech.qquan.idle.service.IdleBaseService;
import com.qqtech.qquan.pub.dao.PubCommentDao;
import com.qqtech.qquan.pub.dao.PubConcernDao;
import com.qqtech.qquan.pub.enums.PubCommentStatusDictEnum;
import com.qqtech.qquan.pub.model.PubComment;
import com.qqtech.qquan.pub.model.PubCommentQuery;
import com.qqtech.qquan.pub.model.PubCommentVo;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.user.dao.UserTopicDao;
import com.qqtech.qquan.user.model.UserTopic;

@Service
public class PubCommentServiceImpl extends BaseServiceImpl<PubComment> implements PubCommentService {
	public static final Logger log = LoggerFactory.getLogger(PubCommentServiceImpl.class);

	@Resource
	private PubCommentDao pubCommentDao;
	@Resource
	private PubConcernDao pubConcernDao;
	@Resource
	private CircleTopicDao CircleTopicDao;
	@Resource
	private CommunityMessageDao communityMessageDao;
	@Resource
	private UserTopicDao userTopicDao;
	@Resource
	private ActivityBaseService activityBaseService;
	@Resource
	private IdleBaseService idleBaseService;
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private PubCommentService pubCommentService;
	@Resource
	private PushBizzService pushBizzService;
	@Resource
	private CommunityMessageService communityMessageService;
	@Resource
	private HiCommunityService hiCommunityService;
	@Resource
	private HiCircleService hiCircleService;

	@Override
	protected BaseDao<PubComment> getBaseDao() {
		return pubCommentDao;
	}

	@Override
	public int getCommentCountByBizz(int bizzId, String bizzType) {
		PubComment comment = new PubComment();
		comment.setBizzId(bizzId);
		comment.setBizzType(bizzType);
		comment.setStatus(PubCommentStatusDictEnum.NORMAL.getCode());
		int count = pubCommentDao.queryCount(comment);
		return count;
	}

	@Override
	public List<PubCommentVo> queryCommentListByBizz(int bizzId, String bizzType) {
		PubCommentQuery pubCommentQuery = new PubCommentQuery();
		pubCommentQuery.setBizzType(bizzType);
		pubCommentQuery.setBizzId(bizzId);
		pubCommentQuery.setStatus(PubCommentStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "newAt"));
		List<PubCommentVo> commentList = pubCommentDao.queryList(pubCommentQuery, pageKit);
		return commentList;
	}

	@Override
	public Integer getBeUserId(int bizzId, String bizzType) {
		if (BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(bizzType)) {
			CircleTopic circleTopic = CircleTopicDao.getById(bizzId);
			if (circleTopic != null) {
				return circleTopic.getCreatorId();
			}
		} else if (BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(bizzType)) {
			CommunityMessage communityMessage = communityMessageDao.getById(bizzId);
			if (communityMessage != null) {
				return communityMessage.getCreatorId();
			}
		} else if (BizzLevel2Enum.USER_TOPIC.getCode().equals(bizzType)) {
			UserTopic userTopic = userTopicDao.getById(bizzId);
			if (userTopic != null) {
				return userTopic.getCreatorId();
			}
		} else if (BizzLevel1Enum.ACTIVITY.getCode().equals(bizzType)) {
			ActivityBaseVo activityBaseVo = activityBaseService.getById(bizzId);
			if (activityBaseVo != null) {
				return activityBaseVo.getCreatorId();
			}
		} else if (BizzLevel1Enum.IDLE.getCode().equals(bizzType)) {
			IdleBaseVo idleBaseVo = idleBaseService.getById(bizzId);
			if (idleBaseVo != null) {
				return idleBaseVo.getCreatorId();
			}
		}
		return null;
	}

	@Override
	public int handleAdd(PubComment pubComment) {
		if (pubComment.getBizzType().equals(BizzLevel2Enum.CIRCLE_TOPIC.getCode())) {// 圈子话评论数量+1
			circleTopicService.increaseComment(pubComment.getBizzId(), 1);
			CircleTopicVo circleTopicVo=circleTopicService.getById(pubComment.getBizzId());
			if(circleTopicVo.getCreatorId()!=pubComment.getCreatorId().intValue()){//评论不是自己的圈子话题奖励
				//荣誉系统-ryz
				hiCircleService.commentTopic(pubComment.getCreatorId(), pubComment.getBizzId());
			}
		}else if(pubComment.getBizzType().equals(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode())){//社区大喇叭评论数量+1
			communityMessageService.increaseComment(pubComment.getBizzId(), 1);
			CommunityMessageVo communityMessageVo=communityMessageService.getById(pubComment.getBizzId());
			if(communityMessageVo.getCreatorId()!=pubComment.getCreatorId().intValue()){//评论不是自己的社区话题奖励
				//荣誉系统-ryz
				hiCommunityService.commentMessage(pubComment.getCreatorId(),pubComment.getBizzId());
			}
		}
		Integer beUserId = pubCommentService.getBeUserId(pubComment.getBizzId(), pubComment.getBizzType());
		pubComment.setBeUserId(beUserId);
		pubComment.setStatus(PubCommentStatusDictEnum.NORMAL.getCode());
		int commentId=pubCommentService.insert(pubComment);
		pushBizzService.pubComment(pubComment.getCreatorId(), beUserId, commentId);//推送给业务发布者【有人评论了你的内容】
		Integer beCommentId = pubComment.getBeCommentedId();
		if(beCommentId!=null){//如果是回复别人的评论，推送给被评论人【有人回复了你的评论】
			PubCommentVo pubCommentVo = pubCommentService.getById(beCommentId);
			if(pubCommentVo!=null){
				pushBizzService.reply2Comment(pubComment.getCreatorId(), pubCommentVo.getCreatorId(), commentId);
			}
		}
		return commentId;
	}

	@Override
	public boolean isFirstCommentDynamic(Integer userId, BizzLevel2Enum bizzLevel2) {
		PubCommentQuery commentQ = new PubCommentQuery();
		commentQ.setCreatorId(userId);
		commentQ.setBizzType(bizzLevel2.getCode());
		int count = pubCommentDao.queryCount(commentQ);
		return (count)==0;
	}
	
	@Override
	public List<PubCommentVo> myLastSomeComments(Integer userId, Integer some, BizzLevel2Enum bizzLevel2) {
		PubCommentQuery commentQ = new PubCommentQuery();
		commentQ.setCreatorId(userId);
		if(bizzLevel2!=null){
			commentQ.setBizzType(bizzLevel2.getCode());
		}
		commentQ.setStatus(PubCommentStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(1,some, new PageOrder(Direction.DESC, "newAt"));
		return pubCommentDao.queryList(commentQ, pageKit);
	}
}